﻿using System.Web;
using System.Xml;
using System.Data;
using System.Collections;

using System.Collections.Generic;
using System.IO;
using TRPOP.Common;

namespace TRPOP.CMS.Model
{
    /// <summary>
    /// 系统配置操作方法
    /// </summary>
    public static class SYSConfig
    {
        private static string AdminRoot = SystemInfoHelper.getSystemInfo().CMSRoot + SystemInfoHelper.getSystemInfo().AdminFolderName;

        #region 基本的操作方法

        /// <summary>
        /// 根据变量key 变量类型 获取该变量的信息集合
        /// </summary>
        /// <param name="strVar">变量key</param>
        /// <param name="strParamTypeKey">变量类型key</param>
        /// <returns></returns>
        public static ArrayList GetVarValue(string strVar, string strParamTypeKey)
        {
            string strPath = Common.Util.GetMapPath(AdminRoot + "/config/" + strParamTypeKey + ".config");
            if (!File.Exists(strPath))
            {
                XmlDocument oXmlDoc = new XmlDocument();
                //xml类型说明
                XmlNode oxmlnode = oXmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
                oXmlDoc.AppendChild(oxmlnode);
                //根节点
                XmlElement oxmlParamType = oXmlDoc.CreateElement("", strParamTypeKey, "");
                XmlText oxmlParamTypeText = oXmlDoc.CreateTextNode("");
                oxmlParamType.AppendChild(oxmlParamTypeText);
                oXmlDoc.AppendChild(oxmlParamType);


                //添加items结点
                XmlElement oxmlItems = oXmlDoc.CreateElement("", "items", "");
                XmlText oxmltext = oXmlDoc.CreateTextNode("");
                oxmlItems.AppendChild(oxmltext);
                oxmlParamType.AppendChild(oxmlItems);
                oXmlDoc.Save(strPath);
            }
            if (File.Exists(strPath))
            {
                return ConfigManage.GetValue(strVar, strPath);
            }
            return null;
        }

        /// <summary>
        /// 根据 某 变量属性集合、属性值 集合来保存某变量信息 
        /// </summary>
        /// <param name="strAttribute">变量属性集合</param>
        /// <param name="strValue">属性值集合</param>
        /// <param name="strParamTypeKey">变量类型</param>
        /// <returns></returns>
        public static bool SaveVarValue(ArrayList strAttribute, ArrayList strValue, string strParamTypeKey)
        {
            string strPath = Common.Util.GetMapPath(AdminRoot + "/config/" + strParamTypeKey + ".config");
            if (!File.Exists(strPath))
            {
                return false;
            }
            return ConfigManage.SaveConfigValue(strAttribute, strValue, strPath);
        }

        /// <summary>
        /// 根据key删除结点
        /// </summary>
        /// <param name="ArrayKey"></param>
        /// <param name="strParamTypeKey"></param>
        /// <returns></returns>
        public static bool DelConfigValue(ArrayList ArrayKey, string strParamTypeKey)
        {
            string strPath = Common.Util.GetMapPath(AdminRoot + "/config/" + strParamTypeKey + ".config");
            return ConfigManage.DelConfigValue(ArrayKey, strPath);
        }

        /// <summary>
        ///  获取某结点类型下的所有变量集合
        /// </summary>
        /// <param name="strParamTypeKey">变量类型</param>
        /// <param name="NodeListPath">结点路径</param>
        /// <returns></returns>
        public static DataTable GetDataSource(string strParamTypeKey, string NodeListPath)
        {
            string strPath = Common.Util.GetMapPath(AdminRoot + "/config/" + strParamTypeKey + ".config");
            if (!File.Exists(strPath))
            {
                GetVarValue("test", strParamTypeKey);
                return null;
            }
            return ConfigManage.GetDataSource(strPath, NodeListPath);
        }

        /// <summary>
        /// 根据文件名，数据变量名 获取值
        /// </summary>
        /// <param name="strVar"></param>
        /// <param name="strFileName"></param>
        /// <returns></returns>
        public static string GetVarValueFromConfig(string strVar, string strFileName)
        {
            DataTable tableTemp = GetDataSource(strFileName, "//items");
            if (tableTemp != null && tableTemp.Rows.Count > 0)
            {
                foreach (DataRow TempRow in tableTemp.Rows)
                {
                    if (TempRow.Table.Columns.Contains("VarKey") && TempRow["VarKey"].ToString().ToLower().Trim() == strVar.Trim().ToLower())
                    {
                        return TempRow["VarValue"].ToString();
                    }
                }
            }
            return string.Empty;
        }

        #endregion

        #region  参数的获取

        #region 评论参数
        /// <summary>
        /// 评论 Key
        /// </summary>
        public static string RecVar
        {
            get
            {
                return "Recomment";
            }
        }
        /// <summary>
        /// 评论 参数类型Key
        /// </summary>
        public static string RecParamTypeKey
        {
            get
            {
                return "Recomment";
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public static bool RecommentEnableTextReplace
        {
            get
            {
                return GetVarValue(RecVar, RecParamTypeKey)[5].ToString().ToString().Trim() == "true";
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public static string RecommentTextReplace
        {
            get
            {
                return GetVarValue(RecVar, RecParamTypeKey)[6].ToString();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public static string RecommentTextReplaceList
        {
            get
            {
                return GetVarValue(RecVar, RecParamTypeKey)[7].ToString();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public static string RecCountPage
        {
            get
            {
                return GetVarValue(RecVar, RecParamTypeKey)[8].ToString();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public static string RecommentSubmitUrl
        {
            get
            {
                return GetVarValue(RecVar, RecParamTypeKey)[9].ToString();
            }
        }
        /// <summary>
        /// 评论动态访问页面
        /// </summary>
        public static string RecommentListUrl
        {
            get
            {
                return GetVarValue(RecVar, RecParamTypeKey)[10].ToString();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public static string RecommentCheckCodeUrl
        {
            get
            {
                return GetVarValue(RecVar, RecParamTypeKey)[11].ToString();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public static string RecommentrecName
        {
            get
            {
                return GetVarValue(RecVar, RecParamTypeKey)[12].ToString();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public static string RecommentrecDetail
        {
            get
            {
                return GetVarValue(RecVar, RecParamTypeKey)[13].ToString();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public static string Recommentindexid
        {
            get
            {
                return GetVarValue(RecVar, RecParamTypeKey)[14].ToString();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public static string RecommentrecvalidCode
        {
            get
            {
                return GetVarValue(RecVar, RecParamTypeKey)[15].ToString();
            }
        }

        /// <summary>
        /// 评论是否审核
        /// </summary>
        public static bool RecommentAudit
        {
            get
            {
                return GetVarValue(RecVar, RecParamTypeKey)[1].ToString().Trim() == "true";
            }
        }
        /// <summary>
        /// 评论是否启用
        /// </summary>
        public static bool RecommentEnable
        {
            get
            {
                return GetVarValue(RecVar, "Plugin")[2].ToString().Trim().ToLower() == "true";
            }
        }
        #endregion

        #region 广告参数

        /// <summary>
        /// Key
        /// </summary>
        public static string AdsVar
        {
            get
            {
                return "Advertisement";
            }
        }
        /// <summary>
        /// Key
        /// </summary>
        public static string AdsParamTypeKey
        {
            get
            {
                return "Advertisement";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static bool AdsEnable
        {
            get
            {
                return GetVarValue(AdsVar, "Plugin")[2].ToString().Trim().ToLower() == "true";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static string AdvertisementDynamicAccessUrl
        {
            get
            {
                return GetVarValue(AdsVar, AdsParamTypeKey)[2].ToString();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public static string AdvertisementRoot
        {
            get
            {
                return GetVarValue(AdsVar, AdsParamTypeKey)[3].ToString();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public static string AdvertisementTypeDefault
        {
            get
            {
                return GetVarValue(AdsVar, AdsParamTypeKey)[5].ToString();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public static string AdvertisementUpLoadRoot
        {
            get
            {
                return GetVarValue(AdsVar, AdsParamTypeKey)[6].ToString();
            }
        }


        /// <summary>
        /// 
        /// </summary>
        public static string AdvertisementSkipURL
        {
            get
            {
                return GetVarValue(AdsVar, AdsParamTypeKey)[7].ToString();
            }
        }

        #endregion

        #endregion

        #region 手机参数
        /// <summary>
        /// 手机版是否开启
        /// </summary>
        public static bool Mobile_IsMobile
        {
            get
            {
                return SystemParamHelper.GetMobileParamByKey("IsMobile").ToBool();
            }
            set
            {
                SystemParamHelper.SetMobileParamByKey("IsMobile", value.ToString(), string.Empty);
            }
        }

        /// <summary>
        /// 手机版等比压缩图片的宽度
        /// </summary>
        public static int Mobile_MaxWidth
        {
            get
            {
                return SystemParamHelper.GetMobileParamByKey("MobileMaxWidth").ToInt();
            }
        }

        /// <summary>
        /// 手机版等比压缩图片的宽度
        /// </summary>
        public static int Mobile_MaxHeight
        {
            get
            {
                return SystemParamHelper.GetMobileParamByKey("MobileMaxHeight").ToInt();
            }
        }

        /// <summary>
        /// 手机版等比压缩图片的后缀名
        /// </summary>
        public static string Mobile_ExtName
        {
            get
            {
                return SystemParamHelper.GetMobileParamByKey("MobileExtName");
            }
        }
        #endregion

        #region 附件设置
        /// <summary>
        /// 永久删除结点时，删除发布页面
        /// </summary>
        public static bool Attachment_DefaultDelPage
        {
            get
            {
                return SystemParamHelper.GetAttachmentParamByKey("defaultdelpage").ToBool();
            }
            set
            {
                SystemParamHelper.SetAttachmentParamByKey("defaultdelpage", value.ToString(), string.Empty);
            }
        }

        /// <summary>
        /// 永久删除结点或信息时，删除上传图片
        /// </summary>
        public static bool Attachment_DefaultDelImg
        {
            get
            {
                return SystemParamHelper.GetAttachmentParamByKey("defaultdelimg").ToBool();
            }
            set
            {
                SystemParamHelper.SetAttachmentParamByKey("defaultdelimg", value.ToString(), string.Empty);
            }
        }

        /// <summary>
        /// 永久删除结点或信息时，删除上传附件
        /// </summary>
        public static bool Attachment_DefaultDelAttr
        {
            get
            {
                return SystemParamHelper.GetAttachmentParamByKey("defaultdelattr").ToBool();
            }
            set
            {
                SystemParamHelper.SetAttachmentParamByKey("defaultdelattr", value.ToString(), string.Empty);
            }
        }

        /// <summary>
        /// 上传图片大小限制(字节)
        /// </summary>
        public static int Attachment_UpImageSize
        {
            get
            {
                int intMax = SystemParamHelper.GetAttachmentParamByKey("UpImageSize").ToInt();

                int intSysMax = 10240 * 1024;

                return intMax * 1024 > intSysMax ? intSysMax : intMax;
            }
            set
            {
                SystemParamHelper.SetAttachmentParamByKey("UpImageSize", value.ToString(), string.Empty);
            }
        }

        /// <summary>
        /// 上传图片类型限制
        /// </summary>
        public static string Attachment_UpImageType
        {
            get
            {
                return SystemParamHelper.GetAttachmentParamByKey("UpImageType");
            }
            set
            {
                SystemParamHelper.SetAttachmentParamByKey("UpImageType", value.ToString(), string.Empty);
            }
        }

        /// <summary>
        /// 图片压缩是否等比压缩（即：(留白)不损失原图质量）
        /// </summary>
        public static bool Attachment_blnPreserverAspectRatio
        {
            get
            {
                return SystemParamHelper.GetAttachmentParamByKey("blnPreserverAspectRatio").ToBool();
            }
            set
            {
                SystemParamHelper.SetAttachmentParamByKey("blnPreserverAspectRatio", value.ToString(), string.Empty);
            }
        }

        /// <summary>
        /// 上传图片压缩缩略图列表
        /// </summary>
        public static string Attachment_ImgReSave
        {
            get
            {
                return SystemParamHelper.GetAttachmentParamByKey("ImgReSave");
            }
            set
            {
                SystemParamHelper.SetAttachmentParamByKey("ImgReSave", value.ToString(), string.Empty);
            }
        }

        /// <summary>
        /// 上传图片的宽度限制，大于此宽度则自动压缩
        /// </summary>
        public static int Attachment_UpImageMaxWidth
        {
            get
            {
                int intMax = SystemParamHelper.GetAttachmentParamByKey("UpImageMaxWidth").ToInt();

                if (intMax <= 0)
                    intMax = 1000;

                return intMax;
            }
            set
            {
                SystemParamHelper.SetAttachmentParamByKey("UpImageMaxWidth", value.ToString(), string.Empty);
            }
        }

        /// <summary>
        /// 上传附件大小限制
        /// </summary>
        public static int Attachment_UpAttachSize
        {
            get
            {
                int intMax = SystemParamHelper.GetAttachmentParamByKey("UpAttachSize").ToInt();

                int intSysMax = 10240 * 1024;

                return intMax * 1024 > intSysMax ? intSysMax : intMax;
            }
            set
            {
                SystemParamHelper.SetAttachmentParamByKey("UpAttachSize", value.ToString(), string.Empty);
            }
        }

        /// <summary>
        /// 上传附件类型限制
        /// </summary>
        public static string Attachment_UpAttachType
        {
            get
            {
                return SystemParamHelper.GetAttachmentParamByKey("UpAttachType");
            }
            set
            {
                SystemParamHelper.SetAttachmentParamByKey("UpAttachType", value.ToString(), string.Empty);
            }
        }

        /// <summary>
        /// 文件上传命名规则
        /// </summary>
        public static string Attachment_UploadFileNameReg
        {
            get
            {
                return SystemParamHelper.GetAttachmentParamByKey("UploadFileNameReg");
            }
            set
            {
                SystemParamHelper.SetAttachmentParamByKey("UploadFileNameReg", value.ToString(), string.Empty);
            }
        }
        #endregion

        #region 缓存参数
        static bool m_Cache_Enable = true;
        /// <summary>
        /// 是否开启缓存
        /// </summary>
        public static bool Cache_Enable
        {
            get
            {
                return m_Cache_Enable;
            }
            set
            {
                m_Cache_Enable = value;
            }
        }
        #endregion

        #region 系统变量

        #region 系统基本变量
        /// <summary>
        /// 网站域名
        /// </summary>
        public static string SystemVar_SiteURL
        {
            get
            {
                return SystemVarInfoHelper.GetSystemVarbyKey("SITE_URL"); //(GetVarValueForCMSVar("SITE_URL")); 
            }
        }

        /// <summary>
        /// 网站首页文件名
        /// </summary>
        public static string SystemVar_IndexPageName
        {
            get
            {
                return SystemVarInfoHelper.GetSystemVarbyKey("Index_Name");//(GetVarValueForCMSVar("Index_Name")); 
            }
        }

        /// <summary>
        /// 网站名称
        /// </summary>
        public static string SystemVar_SiteName
        {
            get
            {
                return SystemVarInfoHelper.GetSystemVarbyKey("SITE_NAME");//(GetVarValueForCMSVar("SITE_NAME")); 
            }
        }

        /// <summary>
        /// 缓存时间
        /// </summary>
        public static double SystemVar_CahceSeconds
        {
            get
            {
                double dbCahceSeconds = SystemVarInfoHelper.GetSystemVarbyKey("CahceSeconds").ToDouble();
                if (dbCahceSeconds <= 0)
                    return 60 * 10;
                else
                    return dbCahceSeconds;
            }
        }

        /// <summary>
        /// 模板地址
        /// </summary>
        public static string SystemVar_Templatepath
        {
            get
            {
                string strTemplate = SystemVarInfoHelper.GetSystemVarbyKey("templatepath"); // GetVarValueForCMSVar("admin_templatepath");

                if (strTemplate.IsEmpty())
                    return @"/templates";
                else
                    return strTemplate;
            }
        }

        /// <summary>
        /// 信息统计页面 
        /// </summary>
        public static string SystemVar_InfoClickSubmitURL
        {
            get
            {
                return SystemVarInfoHelper.GetSystemVarbyKey("InfoClickSubmit");//(GetVarValueForCMSVar("InfoClickSubmit"));
            }
        }

        /// <summary>
        /// 自动分页单页内容长度
        /// </summary>
        public static int SystemVar_ContentPageLength
        {
            get
            {
                return SystemVarInfoHelper.GetSystemVarbyKey("ContentPageLength").ToInt();
            }
        }

        /// <summary>
        /// 请n页静态
        /// </summary>
        public static int SystemVar_listPageCount
        {
            get
            {
                return SystemVarInfoHelper.GetSystemVarbyKey("listPageCount").ToInt();
            }
        }

        /// <summary>
        /// 列表页动态访问地址 
        /// </summary>
        public static string SystemVar_listAutoPage
        {
            get
            {
                return SystemVarInfoHelper.GetSystemVarbyKey("listAutoPage"); //GetVarValueForCMSVar("listAutoPage");
            }
        }

        /// <summary>
        /// 内容页动态访问地址 
        /// </summary>
        public static string SystemVar_ContentAutoPage
        {
            get
            {
                return SystemVarInfoHelper.GetSystemVarbyKey("ContentAutoPage"); //GetVarValueForCMSVar("ContentAutoPage");
            }
        }

        /// <summary>
        /// 通用页面存放址地
        /// </summary>
        public static string SystemVar_NodeIndexPage
        {
            get
            {
                return SystemVarInfoHelper.GetSystemVarbyKey("NodeIndexPage"); //GetVarValueForCMSVar("NodeIndexPage"); 
            }
        }

        /// <summary>
        /// 搜索页URL
        /// </summary>
        public static string SystemVar_PageSearchURL
        {
            get
            {
                return SystemVarInfoHelper.GetSystemVarbyKey("PageSearchURL"); //= GetVarValueForCMSVar("PageSearchURL");
            }
        }

        /// <summary>
        /// 搜索模板路径
        /// </summary>
        public static string SystemVar_PageSearchTemplate
        {
            get
            {
                return SystemVarInfoHelper.GetSystemVarbyKey("PageSearchTemplate"); //= GetVarValueForCMSVar("PageSearchTemplate");
            }
        }

        /// <summary>
        /// 页面提示模板路径
        /// </summary>
        public static string SystemVar_PagePromotTemplate
        {
            get
            {
                return SystemVarInfoHelper.GetSystemVarbyKey("PagePromotTemplate"); //= GetVarValueForCMSVar("PagePromotTemplate");
            }
        }

        /// <summary>
        /// 搜索模板 关键字 input Name
        /// </summary>
        public static string SystemVar_SearchFormInputName
        {
            get
            {
                return SystemVarInfoHelper.GetSystemVarbyKey("SearchFormInputName"); //= GetVarValueForCMSVar("SearchFormInputName");
            }
        }
        #endregion

        #region 资源发布、内容发布 变量
        /// <summary>
        /// 默认资源发布点
        /// </summary>
        public static string SystemVar_DefaultResourcePSN
        {
            get
            {

                string strReturn = SystemVarInfoHelper.GetSystemVarbyKey("DefaultResourcePSN");//(GetVarValueForCMSVar("DefaultResourcePSN"));

                if (string.IsNullOrEmpty(strReturn))
                    strReturn = "{PSN:1}";
                return strReturn;
            }
        }

        /// <summary>
        /// 默认资源发布URL
        /// </summary>
        public static string SystemVar_DefaultResourcePSNURL
        {
            get
            {
                string strReturn = SystemVarInfoHelper.GetSystemVarbyKey("DefaultResourcePSNURL");//(GetVarValueForCMSVar("DefaultResourcePSNURL"));

                if (string.IsNullOrEmpty(strReturn))
                    strReturn = "{PSN-URL:1}";
                return strReturn;
            }
        }

        /// <summary>
        /// 默认内容发布点
        /// </summary>
        public static string SystemVar_DefaultContentPSN
        {
            get
            {
                string strReturn = SystemVarInfoHelper.GetSystemVarbyKey("DefaultContentPSN");//(GetVarValueForCMSVar("DefaultContentPSN"));

                if (string.IsNullOrEmpty(strReturn))
                    strReturn = "{PSN:1}/html";
                return strReturn;
            }
        }

        /// <summary>
        /// 默认内容发布URL
        /// </summary>
        public static string SystemVar_DefaultContentPSNURL
        {
            get
            {
                string strReturn = SystemVarInfoHelper.GetSystemVarbyKey("DefaultContentPSNURL");//(GetVarValueForCMSVar("DefaultContentPSNURL"));

                if (string.IsNullOrEmpty(strReturn))
                    strReturn = "{PSN-URL:1}/html";
                return strReturn;
            }
        }
        #endregion

        #region 发布管理相关参数
        /// <summary>
        /// 发布管理页标题长度
        /// </summary>
        public static int SystemVar_ListTitleLength
        {
            get
            {
                int inGetvalue = SystemVarInfoHelper.GetSystemVarbyKey("list_titlelength").ToInt();//GetVarValueForCMSVar("list_titlelength")
                return inGetvalue > 0 ? inGetvalue : 10;
            }

        }

        /// <summary>
        /// 发布管理单页显示记录
        /// </summary>
        public static int SystemVar_AdminContentPageNum
        {
            get
            {
                int inGetvalue =SystemVarInfoHelper.GetSystemVarbyKey("adminContentPageNum").ToInt();//GetVarValueForCMSVar("adminContentPageNum")
                return inGetvalue > 0 ? inGetvalue : 10; 
            }
        }

        /// <summary>
        /// 发布管理单页内容显示最大值
        /// </summary>
        public static int SystemVar_PubPageListMax
        {
            get
            {
                int inGetvalue = SystemVarInfoHelper.GetSystemVarbyKey("PubPageListMax").ToInt();//GetVarValueForCMSVar("PubPageListMax")
                return inGetvalue > 0 ? inGetvalue : 30; 
            }
        }
        #endregion

        #endregion

        #region 编辑器绑定 内容分页生成器
        /// <summary>
        /// 所有的内容编辑器
        /// </summary>
        public static DataTable ContentEditors
        {
            get
            {
                string strPath = Common.Util.GetMapPath(SystemConst.ConfigRootName + SystemConst.SystemVar_ContentEditorFileName);

                if (!File.Exists(strPath))
                {
                    return null;
                }

                return ConfigManage.GetDataSource(strPath, "//items");
            }
        }

        /// <summary>
        /// 所有的内容分页
        /// </summary>
        public static DataTable ContentPagers
        {
            get
            {
                string strPath = Common.Util.GetMapPath(SystemConst.ConfigRootName + SystemConst.SystemVar_ContentPagerFileName);

                if (!File.Exists(strPath))
                {
                    return null;
                }

                return ConfigManage.GetDataSource(strPath, "//items");
            }
        }
        #endregion
    }
}